
import {request, showMsg} from '../../../utils/util.js';
import { JsDetail,JsSave } from '../../../config/api.js';

const app = getApp();
var random = Math.random();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    get_view: false,
    create_editor: false,
    set_data:{
      id:0,
      tag:'coffee',
      content:''
    },
    navH:0,
    items: [
      {id: 1, value: 'coffee', name: '日记'},
      {id: 2, value: 'linux', name: '代码'}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that= this;
    let navH = app.globalData.navHeight;
    /*顶部导航高度*/
    that.setData({
      navH: navH
    });
    console.log(options.id);
    if(options.id>0){
      this.fetchDetail(options.id);    
    }else{
      this.setData({
        set_data:{
          id:0,
          tag:'coffee',
          content:''
        }
      }) 
    }
  },

  // onShow: function(e){
  //   this.onLoad();
  // },

  radioChange(e) {
    const items = this.data.items;
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }
    this.setData({
      items,
      ['set_data.tag'] : e.detail.value
    })
  },

  //监控输入
  input_text(e) {
    let html = e.detail.html;
    this.setData({
      ['set_data.content'] : html
    });
    console.log(html);
  },

  // 编辑时获取详情
  fetchDetail: function (id) {
    let _this = this;
    request(JsDetail+'?id='+id, 'GET').then(function (res) {
      if (res.s == 1) {
        var content = res.d.info.content;
        _this.setData({
          detail:res.d.info,
          hidden: true,
          get_view: true,
          set_data: {
            id : res.d.info.id,
            tag : res.d.info.tag,
            content : content
          }
        });

        if(_this.data.get_view==true && _this.data.create_editor==true){
          _this.editorCtx.setContents({
            html: content
          });
        }

      }
    })       
  },


  article_save: function(){
    var that = this;
    let set_data = that.data.set_data;
    request(JsSave,set_data,'POST').then(res => {
      if (res.s == 1) {
          wx.showToast({
            title: '保存成功！',
            icon: 'success', //没有图标
            duration: 1500 //持续的时间
          });
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/index/index/index',
            })
          },2000);
      } else {
        showToast('保存失败!',2);
      }
    });
  },


  // 编辑器方法===============================================================================================
  _onEditorReady: function () {
    const _this = this;
    _this.createSelectorQuery().select('#editor').context(function (res) {
      _this.editorCtx = res.context;
      _this.setData({
        create_editor : true
      });
      if(_this.data.get_view==true && _this.data.create_editor==true){
        _this.editorCtx.setContents({
          html: _this.data.set_data.content
        });
      }
    }).exec();  
  },

  //添加粗体样式
  _addBold: function () {
    this.editorCtx.format("bold")
  },
  //设置标题
  _addHeader: function (e) {
    let headerType = e.currentTarget.dataset.header;
    this.editorCtx.format("header", headerType)
  },
  //设置列表
  _addList: function (e) {
    let listType = e.currentTarget.dataset.list;
    this.editorCtx.format("list", listType);
  },
  //撤销
  _undo: function () {
    this.editorCtx.undo();
  },
  //监控输入
  _onInputting: function (e) {
    let html = e.detail.html;
    let text = e.detail.text;
    this.setData({
      ['set_data.content'] : html
    });
    console.log(html);
  }
})